package com.playball.lottery.customview;

import java.util.ArrayList;
import java.util.HashMap;

import android.R.integer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.frm.buyhall.BDxhlbActivity;
import com.playball.lottery.frm.buyhall.CQSSCListActivity;
import com.playball.lottery.frm.buyhall.DltxhlbActivity;
import com.playball.lottery.frm.buyhall.ElvSelFiveListActivity;
import com.playball.lottery.frm.buyhall.ElvYunDJListActivity;
import com.playball.lottery.frm.buyhall.Fc3dListActivity;
import com.playball.lottery.frm.buyhall.JcBasketballxhlbActivity;
import com.playball.lottery.frm.buyhall.JcfootxhlbActivity;
import com.playball.lottery.frm.buyhall.Kuai3ListActivity;
import com.playball.lottery.frm.buyhall.N7lxhlbActivity;
import com.playball.lottery.frm.buyhall.N7xxhlbActivity;
import com.playball.lottery.frm.buyhall.Pailie3ListActivity;
import com.playball.lottery.frm.buyhall.Pailie5ListActivity;
import com.playball.lottery.frm.buyhall.SsqxhlbActivity;
import com.playball.lottery.util.JCUtil;



public class YZJSAdapter extends BaseAdapter {

	private class MyViewHolder {
		TextView Ball_red;
		TextView Ball_blue;
		TextView Ball_money;
	}

	private class MyElvSelFive {
		TextView ball;
		TextView ball_money;
	}

	private class MyJcFoot {
		TextView tv_matchname;
		TextView tv_item_no_endtime;
		View ll1; 
		TextView hteam;
		TextView vteam;
		Button rq1;
		Button sbut;
		Button pbut;
		Button fbut;
		View ll2; 
		Button rq2;
		Button rqsbut;
		Button rqpbut;
		Button rqfbut;
	}

	private ArrayList<HashMap<String, Object>> mAppList;
	private LayoutInflater mInflater;
	private Context mContext;
	private String[] keyString;
	private int[] valueViewID;
	private MyViewHolder myholder;
	private MyElvSelFive myElv;
	private MyJcFoot myFoot;

	private String str_hteam;
	private String str_vteam;
	private String str_sp;
	private String str_rqsp;
	private String tv_matchname;
	private String tv_item_no_endtime;
	private String rq2;

	public YZJSAdapter(ArrayList<HashMap<String, Object>> appList) {
		mAppList = appList;
	}

	public YZJSAdapter(Context c, ArrayList<HashMap<String, Object>> appList, int resource, String[] from, int[] to) {
		mAppList = appList;
		mContext = c;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		keyString = new String[from.length];
		valueViewID = new int[to.length];
		System.arraycopy(from, 0, keyString, 0, from.length);
		System.arraycopy(to, 0, valueViewID, 0, to.length);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
			if (convertView != null) {
				myFoot = (MyJcFoot) convertView.getTag();
			} else {
				convertView = mInflater.inflate(R.layout.jczq_dgp_item2, null);
				myFoot = new MyJcFoot();
				myFoot.tv_matchname = (TextView) convertView.findViewById(valueViewID[0]);
				myFoot.tv_item_no_endtime = (TextView) convertView.findViewById(valueViewID[1]);
				myFoot.hteam = (TextView) convertView.findViewById(valueViewID[2]);
				myFoot.vteam = (TextView) convertView.findViewById(valueViewID[3]);
				myFoot.ll1 = (View)convertView.findViewById(valueViewID[4]);
				myFoot.rq1 = (Button) convertView.findViewById(valueViewID[5]);
				myFoot.sbut = (Button) convertView.findViewById(valueViewID[6]);
				myFoot.pbut = (Button) convertView.findViewById(valueViewID[7]);
				myFoot.fbut = (Button) convertView.findViewById(valueViewID[8]);
				myFoot.ll2 = (View)convertView.findViewById(valueViewID[9]);
				myFoot.rq2 = (Button) convertView.findViewById(valueViewID[10]);
				myFoot.rqsbut = (Button) convertView.findViewById(valueViewID[11]);
				myFoot.rqpbut = (Button) convertView.findViewById(valueViewID[12]);
				myFoot.rqfbut = (Button) convertView.findViewById(valueViewID[13]);
				convertView.setTag(myFoot);
			}

			try {
				myFoot.sbut.setSelected(false);
				myFoot.pbut.setSelected(false);
				myFoot.fbut.setSelected(false);
				myFoot.rqsbut.setSelected(false);
				myFoot.rqpbut.setSelected(false);
				myFoot.rqfbut.setSelected(false);
				myFoot.rq1.setSelected(false);
				myFoot.rq2.setSelected(false);
				myFoot.rq1.setClickable(false);
				myFoot.rq2.setClickable(false);
				HashMap<String, Object> appInfo = mAppList.get(position);
				if (appInfo != null) {
//					tv_matchname = (String) appInfo.get(keyString[0]);
//					tv_item_no_endtime = (String) appInfo.get(keyString[1]);
					str_hteam = (String) appInfo.get(keyString[2]);
					str_vteam = (String) appInfo.get(keyString[3]);
					str_sp = (String) appInfo.get(keyString[4]);
//					str_rqsp = (String) appInfo.get(keyString[5]);
//					rq2 = (String) appInfo.get(keyString[6]);
					
//					myFoot.tv_matchname.setText(tv_matchname);
//					myFoot.tv_item_no_endtime.setText(tv_item_no_endtime);
					myFoot.hteam.setText(str_hteam);
					myFoot.vteam.setText(str_vteam);
					myFoot.rq1.setText("0");
					
					if(str_sp != null && str_rqsp == null){
						str_sp = JCUtil.zhuanHuanSPGeShi(str_sp);
					}else
					if(str_rqsp != null && str_sp == null){
						str_rqsp = JCUtil.zhuanHuanSPGeShi(str_rqsp);
					}else{
						str_sp = JCUtil.zhuanHuanSPGeShi(str_sp);
						str_rqsp = JCUtil.zhuanHuanSPGeShi(str_rqsp);
					}
					
					if("".equals(str_sp) || null == str_sp){
						myFoot.ll1.setVisibility(View.GONE);
						if(Integer.valueOf(rq2) > 0){
							myFoot.rq2.setText("+"+rq2);
						}else{
							myFoot.rq2.setText(rq2);
						}
						if (containsAny(str_rqsp, "3")) {
							myFoot.rqsbut.setSelected(true);
						}
						if (containsAny(str_rqsp, "1")) {
							myFoot.rqpbut.setSelected(true);
						}
						if (containsAny(str_rqsp, "0")) {
							myFoot.rqfbut.setSelected(true);
						}
					}else if("".equals(str_rqsp) || null == str_rqsp){
						myFoot.ll2.setVisibility(View.GONE);
						if (containsAny(str_sp, "3")) {
							myFoot.sbut.setSelected(true);
						}
						if (containsAny(str_sp, "1")) {
							myFoot.pbut.setSelected(true);
						}
						if (containsAny(str_sp, "0")) {
							myFoot.fbut.setSelected(true);
						}
					}else{
						
						if (containsAny(str_rqsp, "3")) {
							myFoot.rqsbut.setSelected(true);
						}
						if (containsAny(str_rqsp, "1")) {
							myFoot.rqpbut.setSelected(true);
						}
						if (containsAny(str_rqsp, "0")) {
							myFoot.rqfbut.setSelected(true);
						}
						if (containsAny(str_sp, "3")) {
							myFoot.sbut.setSelected(true);
						}
						if (containsAny(str_sp, "1")) {
							myFoot.pbut.setSelected(true);
						}
						if (containsAny(str_sp, "0")) {
							myFoot.fbut.setSelected(true);
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
//				e.printStackTrace();
			}
		return convertView;
	}



	@Override
	public int getCount() {
		return mAppList.size();
	}

	@Override
	public Object getItem(int position) {
		return mAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	
	
	public ArrayList<ChildDTO> GetFootList() {
		ArrayList<ChildDTO> newlist = new ArrayList<ChildDTO>();
		for (int i = 0; i < mAppList.size(); i++) {
			String mId = (String) mAppList.get(i).get(keyString[0]);
			String endtime = (String) mAppList.get(i).get(keyString[1]);
			String str_hteam = (String) mAppList.get(i).get(keyString[2]);
			String str_vteam = (String) mAppList.get(i).get(keyString[3]);
			String str_sp = (String) mAppList.get(i).get(keyString[4]);
			String str_rqsp = (String) mAppList.get(i).get(keyString[5]);
			String rq2 = (String) mAppList.get(i).get(keyString[6]);
			String str_time = (String) mAppList.get(i).get("tip_time");
			ChildDTO childdto = new ChildDTO();
			childdto.mId = mId;
			childdto.eTime = endtime;
			childdto.hTeam = str_hteam;
			childdto.vTeam = str_vteam;
			childdto.sPspf = str_sp;
			childdto.sPRqspf = str_rqsp;
			childdto.eQ = rq2;
			childdto.eTime = str_time;
			newlist.add(childdto);
		}
		return newlist;
	}
	
	

	// 判断字符串是否有特点的字符
	static boolean containsAny(String str, String searchChars) {
		return str.contains(searchChars);
	}
}